﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KaytechSoftware.Cms.App.Areas.Admin.Models.Entities;
using KaytechSoftware.Cms.App.Areas.Admin.Models.Filters;
using KaytechSoftware.Cms.App.Common;
using KaytechSoftware.Cms.Services.Dto;
using KaytechSoftware.Cms.Services.Interfaces;
using KaytechSoftware.Cms.Services.Interfaces.Entity;

namespace KaytechSoftware.Cms.App.Areas.Admin.Controllers
{
    public class PhotoAlbumController : GridController<PhotoAlbumFiltersModel, PhotoAlbumModel, PhotoAlbumDto>
    {
        private readonly IPhotoAlbumService _photoAlbumService;
        private readonly IPageService _pageService;

        public PhotoAlbumController(IPhotoAlbumService photoAlbumService, IPageService pageService) : base(photoAlbumService)
        {
            _photoAlbumService = photoAlbumService;
            _pageService = pageService;
        }

        public override ActionResult Add()
        {
            var pageDtos = _pageService.GetAll();
            var model = CreateEntityModel();
            model.RegisterCollection(CollectionKey.Page, pageDtos, p => p.Id, p => p.Title);

            var selectedPageId = Session[SessionKeys.SelectedPage] as long?;
            if (selectedPageId.HasValue)
                model.PageId = selectedPageId.Value;

            return Details(model);
        }

        public override ActionResult Edit(long id)
        {
            var photoAlbumDto = _photoAlbumService.GetDtoById(id);
            var pageDtos = _pageService.GetAll();

            var model = CreateEntityModel();
            model.FromDto(photoAlbumDto);
            model.RegisterCollection(CollectionKey.Page, pageDtos, p => p.Id, p => p.Title);

            return Details(model);
        }

        public override void OnPreSave(PhotoAlbumModel editModel)
        {
            Session[SessionKeys.SelectedPage] = editModel.PageId;
        }

        public override PhotoAlbumFiltersModel RestoreOrGetDefaultFilters()
        {
            var filters = new PhotoAlbumFiltersModel();
            var selectedPageId = Session[SessionKeys.SelectedPage] as long?;
            if (selectedPageId.HasValue)
            {
                filters.SelectedPageId = selectedPageId.Value;
            }
            else
            {
                var pageDtos = _pageService.GetAll();
                if (pageDtos.Any())
                    filters.SelectedPageId = pageDtos.OrderBy(p => p.Id).First().Id;
            }

            RegisterCollectionsForFilters(filters);
            return filters;
        }

        public override void RegisterCollectionsForFilters(PhotoAlbumFiltersModel filters)
        {
            var pageDtos = _pageService.GetAll();
            filters.RegisterCollection(CollectionKey.Page, pageDtos, p => p.Id, p => p.Title);
            Session[SessionKeys.SelectedPage] = filters.SelectedPageId;
        }

        protected override IList<PhotoAlbumDto> Filter(PhotoAlbumFiltersModel filtersModel)
        {
            var items = _photoAlbumService.GetAllForPage(filtersModel.SelectedPageId);
            return items;
        }

        public ActionResult PhotoList(long id)
        {
            Session[SessionKeys.SelectedPhotoAlbum] = id;
            return RedirectToAction("List", "Photo", new {area = "Admin"});
        }
    }
}
